package com.example.admin.yuejian21.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.example.admin.yuejian21.Adapter.ActivityAdapter;
import com.example.admin.yuejian21.Bean.Activity;
import com.example.admin.yuejian21.Bean.Contest;
import com.example.admin.yuejian21.Bean.Master;
import com.example.admin.yuejian21.R;
import com.example.admin.yuejian21.Service.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class YjFragment extends Fragment {


    private Button activity,master,contest;
    private FrameLayout home_frame;
    private FragmentTransaction homeaction;
    private FragmentManager fragmentManager;
    private ActivityFragment activityFragment;
    private MasterFragment masterFragment;
    private ContestFragment contestFragment;
    private List<ImageView> images;
    private int[] imgResource={R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four};
    private ViewPager home_vp;
    private View view;
    //当前图片的位置
    private int current;
    private Context context;
    //上一次图片的位置
    private int oldpos;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_yj, container, false);
        init();
        event();
        lunbo();
        return view;
    }

    private void event() {
        activity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replace(activityFragment);
            }
        });
        master.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                replace(masterFragment);
            }
        });
        contest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replace(contestFragment);
            }
        });

    }

    private void lunbo() {
        home_vp.setAdapter(pageadapter);
    }

    public void  init(){
        activity=(Button)view.findViewById(R.id.activity);
        master=(Button)view.findViewById(R.id.master);
        contest=(Button)view.findViewById(R.id.contest);
        home_frame=(FrameLayout)view.findViewById(R.id.home_frame);
        activityFragment=new ActivityFragment();
        masterFragment=new MasterFragment();
        contestFragment=new ContestFragment();
        context=getActivity();
        fragmentManager=getChildFragmentManager();
        homeaction=fragmentManager.beginTransaction();
        homeaction.add(R.id.home_frame,activityFragment);
        homeaction.commit();
        home_vp=(ViewPager)view.findViewById(R.id.yj_vp);

        //创建List对象，用于装载ImageView对象
        images=new ArrayList<ImageView>();
        for (int i=0;i<imgResource.length;i++)
        {
            ImageView imageView=new ImageView(getActivity());
            imageView.setBackgroundResource(imgResource[i]);
            images.add(imageView);
        }
    }

    PagerAdapter pageadapter=new PagerAdapter() {
        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(images.get(position));
            return images.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(images.get(position));
        }
    };
    class  PageTask implements Runnable{
        @Override
        public void run() {
            current=(current+1)%imgResource.length;
            handler.sendEmptyMessage(current);
        }
    }
    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            home_vp.setCurrentItem(current);
        };
    };

    @Override
    public void onStart() {
        super.onStart();
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                current=(current+1)%imgResource.length;
                handler.sendEmptyMessage(current);
            }
        },4000,4000);

    }

    private void  replace(Fragment fragment)
    {
        homeaction=fragmentManager.beginTransaction();
        homeaction.replace(R.id.home_frame,fragment);
        homeaction.commit();
    }
}
